var initdata = function (that) {
  var chooseadress = that.data.chooseadress
  for (var i = 0; i < adress.length; i++) {
    chooseadress[i].shows = ""
  }
  that.setData({
    chooseadress: chooseadress
  })
}
const app = getApp()
Page({

  data: {
    model:false,
    delBtnWidth: 180,
    adress:'',
    showPop:false,
    chooseadress:[],
  },

  onLoad: function (options) {
    this.initEleWidth();
    this.getadress();
  },

  onShow: function () {

  },
  showmodel() {  //打开弹窗
    this.setData({
      showPop: true
    })
     this.getadress1();

  },
  trigDetailBox1: function () {  //关闭弹窗
    this.setData({
      showPop: false
    })
  },
  radioval: function (e) { //单选按钮
    let item = e.currentTarget.dataset.item;
    let index = e.currentTarget.dataset.index;
    let adress = this.data.adress;
    let chooseadress = this.data.chooseadress;
    item.num='';
   
    let _that = this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/egoods/egoods/addEsonorder', //分配地址
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        customer: wx.getStorageSync('customer'),
        addid:item.id,
        num:0
      },
      success: function (res) {
        if(res.data.code==0){
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          item.esonid = res.data.data.esonid
          console.log(item)
          chooseadress.push(item);
          _that.setData({
            adress: adress,
            chooseadress: chooseadress,
            showPop: false
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
        
      },

    })

  },
  getadress() {  //获取地址
    let that = this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/oneself/getAddressList',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid')
      },
      success: function (res) {

          console.log(res)
          var attr = [];
          for (var i = 0; i < res.data.data.length; i++) {
            if (res.data.data[i].status == 1) {
              console.log(res.data.data[i])
              attr.push(res.data.data[i])                           
              wx.request({   //传后台
                url: app.globalData.url + '/jxb/egoods/egoods/addEsonorder', //分配地址
                method: 'post',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  customer: wx.getStorageSync('customer'),
                  addid: res.data.data[i].id,
                  num: 0
                },
                success: function (res) {
                  if (res.data.code == 0) {
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'none'
                    })
                    that.setData({
                      chooseadress: attr,
                    });

                  } else {
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'none'
                    })
                  }

                },

              })
            
            }
          }

          for (var i = 0; i < res.data.data.length; i++) {
            for (var j = 0; j < that.data.chooseadress.length; j++) {
              if (res.data.data[i].id == that.data.chooseadress[j].id) {
                // arr2.splice(i, 1);
                res.data.data[i].choose = 1;
              }
            }

          }              
        that.setData({
          adress: res.data.data,
        });
      },

    })
  },
  getadress1() {  //获取地址
    let _that = this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/oneself/getAddressList',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid')
      },
      success: function (res) {

        console.log(res)

        for (var i = 0; i < res.data.data.length; i++) {
          for (var j = 0; j < _that.data.chooseadress.length; j++) {
            if (res.data.data[i].id == _that.data.chooseadress[j].id) {
              // arr2.splice(i, 1);
              res.data.data[i].choose = 1;
            }
          }

        }
        _that.setData({
          adress: res.data.data,
        });
      },

    })
  },
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置 
        startX: e.touches[0].clientX
      });
    }
  },
  delet: function (e) {  //删除图片
    var that = this;
    var chooseadress = that.data.chooseadress;
    var index = e.currentTarget.dataset.index;//获取当前长按图片下标
    wx.showModal({
      title: '提示',
      content: '确定要删除此地址吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('点击确定了');
          wx.request({   //传后台
            url: app.globalData.url + '/jxb/egoods/egoods/delEsonorder',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              customer: wx.getStorageSync('customer'),
              esonid: chooseadress[index].esonid,
            },
            success: function (res) {
              if(res.data.code==0){
                chooseadress.splice(index, 1);
                that.setData({
                  chooseadress: chooseadress
                })
                wx.showToast({
                  title: res.data.msg,
                })
              }else{
                wx.showToast({
                  title: res.data.msg,
                })
              }
            },

          })       
        } else if (res.cancel) {
          console.log('点击取消了');
          return false;
        }
        that.setData({
          chooseadress: chooseadress
        });
      }
    })
  },

  getnum(e){ //获取数量
    let index = e.currentTarget.dataset.index;
    var chooseadress = this.data.chooseadress;
    let num = e.detail.value
    chooseadress[index].num = num  
    this.setData({
      chooseadress:chooseadress
    })
    console.log(chooseadress[index])
    let that = this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/egoods/egoods/updateEsonorder',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        customer: wx.getStorageSync('customer'),
        addid: chooseadress[index].id,
        num:num,
        // esonid: chooseadress[index].esonid
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '已分配',
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      },
      error(){
        wx.showToast({
          title:'网络错误',
        })
      }

    })   
  },
  addadress(){
    wx:wx.navigateTo({
      url: '/pages/adress/addadress/addadress',

    })
  },
  submit(){  //提交订单；
    
    let that = this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/eorder/createOrder',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        customer: wx.getStorageSync('customer'),
        uid: wx.getStorageSync('uid'),
        token: wx.getStorageSync('token'),
      },
      success: function (res) {
        if (res.data.code == 0) {
          that.setData({
            model: true
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      },
      error() {
        wx.showToast({
          title: '网络错误',
        })
      }

    })   

  },
  paysucess() { //支付成功跳转
    this.setData({
      model: false
    })
    let that = this;
    wx.request({   //传后台
      url: app.globalData.url + '/jxb/order/isComment',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: wx.getStorageSync('token'),
        uid: wx.getStorageSync('uid'),
      },
      success: function (res) {
        let pstatus = res.data.data.pstatus;
        if (pstatus == '1') {  //已评价
          wx.navigateTo({
            url: '/pages/paymore/paymore',
          })
        } else {
          wx.navigateTo({
            url: '/pages/ratestart/ratestart',
          })
        }
      },

    })
  },
  // 滑动中事件
  touchE: function (e) {
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置 
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离 
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮 
      var txtStyle = "";
      txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
      //获取手指触摸的是哪一项 
      var index = e.currentTarget.dataset.index;
      var chooseadress = this.data.chooseadress;
      chooseadress[index].shows = txtStyle;
      // console.log("1", list[index].shows);
      //更新列表的状态 
      this.setData({
        chooseadress: chooseadress
      });
    } else {
      console.log("2");
    }
  },
  //获取元素自适应后的实际宽度 
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);
      // console.log(scale); 
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error 
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },

})